
package cn.edu.scau.cmi.chenzehan.hibernateframe.DAO;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.chenzehan.hibernateframe.POJO.HibernateUtil;
import cn.edu.scau.cmi.chenzehan.hibernateframe.POJO.Tutor;

public class TutorDAO {

	public void save(Tutor tutor) {
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		try {
			session.save(tutor);
			tx.commit();
		}catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		}
		finally {
			HibernateUtil.closeSession();
		}
	}
	//根据id查找指定id
	public Tutor findById(int tutorID) {
		Tutor Tutor = null;
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		try {
			Tutor = (Tutor) session.get(Tutor.class, tutorID);
			tx.commit();
		}catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		}
		finally {
			HibernateUtil.closeSession();
		}
		return Tutor;
	}
	//根据name查找指定name
	public List<Tutor> findByProperty(String PropertyName,Object value){
		Session session = HibernateUtil.getSession();
		String queryString = "select * from tutor where" + PropertyName + "=?";
		Query queryObject = session.createSQLQuery(queryString).addEntity(Tutor.class);
		queryObject.setParameter(0, value);
		List<Tutor> list = queryObject.list();
		session.close();
		return list;
	}
	//删除用户
	public void delete(Tutor tutor) {
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		try {
			session.delete(tutor);
			tx.commit();
		}catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		}
		finally {
			HibernateUtil.closeSession();
		}
	}
	
	//修改用户信息
	public void update(Tutor tutor) {
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		try {
			session.update(tutor);
			tx.commit();
		}catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		}
		finally {
			HibernateUtil.closeSession();
		}
	}
}
